import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import store from '../store'
import Home from '../views/home/index.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: '',
    redirect:'/user',
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: {
      keepAlive: true, // 是否使用 keep-alive
      grade:true
       }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/index.vue'),
    meta: {
      keepAlive: false, // 是否使用 keep-alive
       }
  },
  {
    path: '/privacy',
    name: 'privacy',
    component: () => import('../views/login/privacy.vue'),
    meta: {
      keepAlive: false, // 是否使用 keep-alive
      notRetainpath:true
       }
  },
  
  {
    path: '/profit',
    name: 'profit',
    component: () => import( '../views/profit/index.vue'),
    meta: {
      keepAlive: false, // 是否使用 keep-alive
      islogin:true
       }
  },
  {
    path: '/drawal',
    name: 'drawal',
    component: () => import( '../views/profit/drawal.vue'),
    meta: {
      keepAlive: false, // 是否使用 keep-alive
      islogin:true
       }
  },
  
  {
    path: '/user',
    name: 'user',
    component: () => import('../views/user/index.vue'),
    meta: {
      keepAlive: false // 是否使用 keep-alive
       }
  },
  {
    path: '/extension',
    name: 'extension',
    component: () => import('../views/user/extension.vue'),
    meta: {
      keepAlive: false // 是否使用 keep-alive
       }
  },
  {
    path: '/poster/:parents_id/:user',
    name: 'poster',
    component: () => import('../views/user/poster.vue'),
    meta: {
      keepAlive: false // 是否使用 keep-alive
       }
  },
  {
    path: '/posterList/:id/:parents_id/:user',
    name: 'posterList',
    component: () => import('../views/user/posterList.vue'),
    meta: {
      keepAlive: false // 是否使用 keep-alive
       }
  },
  {
    path: '/product/:id/:parents_id',
    name: 'product',
    component: () => import(/* webpackChunkName: "product" */ '../views/product/index.vue'),
    meta: {
      // isUseCache: true, // 默认不缓存

      keepAlive: false, // 是否使用 keep-alive
    }
  },
  
  {
    path: '/searchbefore',
    name: 'searchbefore',
    component: () => import(/* webpackChunkName: "product" */ '../views/home/searchbefore.vue'),
    meta: {
      // isUseCache: true, // 默认不缓存

      keepAlive: true // 是否使用 keep-alive

    }
  },
  {
    path: '/search',
    name: 'search',
    component: () => import(/* webpackChunkName: "product" */ '../views/home/search.vue'),
    meta: {
      // isUseCache: true, // 默认不缓存

      keepAlive: true // 是否使用 keep-alive

    }
  },
  {
    path: '/register/:type',
    name: 'register',
    component: () => import(/* webpackChunkName: "product" */ '../views/user/register.vue'),
    meta: {
      // isUseCache: true, // 默认不缓存

      keepAlive: false, // 是否使用 keep-alive
      islogin:true

    }
  },
  {
    path: '/auxiliary',
    name: 'auxiliary',
    component: () => import(/* webpackChunkName: "product" */ '../views/user/auxiliary.vue'),
    meta: {
      // isUseCache: true, // 默认不缓存

      keepAlive: false, // 是否使用 keep-alive
      
    }
  },
  {
    path: '/applylist',
    name: 'applylist',
    component: () => import(/* webpackChunkName: "product" */ '../views/user/applylist.vue'),
    meta: {
      // isUseCache: true, // 默认不缓存

      keepAlive: false // 是否使用 keep-alive

    }
  },
  {
    path: '/team',
    name: 'team',
    component: () => import(/* webpackChunkName: "product" */ '../views/user/team.vue'),
    meta: {
      // isUseCache: true, // 默认不缓存

      keepAlive: false // 是否使用 keep-alive

    }
  },
  {
    path: '/contract/:src',
    name: 'contract',
    component: () => import(/* webpackChunkName: "product" */ '../views/contract/index.vue'),
    meta: {
      // isUseCache: true, // 默认不缓存

      keepAlive: false, // 是否使用 keep-alive
      notRetainpath:true
    }
  },
  
  
]

const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes
})
router.beforeEach((to, from, next) => {
  if(to.meta.islogin&&!store.state.userInfo){
    return next({path:'/login'})
  }
  
  if(to.meta.grade&&store.state.userInfo.F_SecurityLevel!=18){
    return next({path:'/login'})
  }
  if(to.meta.notRetainpath||from.meta.notRetainpath){
    return next()
  }
  store.commit({
    type: "Retainpath",
    path: from.path,
  });
  next()
})
export default router
